<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Tag extends Model
{
    //
    protected $table='tag';

    public function getArticleNum()
    {
        return DB::table('article')->select(DB::raw('count(id) article_num,tag_id'))->where('tag_id','!=','null')->groupby('tag_id')->get();

    }
    /*
     *生成标签下拉选项框
     * */
    public function getTagSelect($tag_id){
        $html = '<select class="form-control" name="tag_id">';
        $html .= '<option value="">--请选择--</option>';
        foreach($this->orderby('id','ASC')->get() as $key => $value){
            if($tag_id == $value->id){
                $html .= '<option value="'.$value->id.'" selected>'.$value->tag_name.'</option>';
            }else{
                $html .= '<option value="'.$value->id.'">'.$value->tag_name.'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }
}
